﻿/*
=======================================================
    UsingWirerope【在用绳档案】
=======================================================
*/

namespace Services
{
    public interface IUsingWireropeMapping : IMapping<UsingWirerope, UsingWireropeModel, UsingWireropeFilterModel>
    { }

    public class UsingWireropeMapping : AutoUsingWireropeMapping, IUsingWireropeMapping
    {
        private readonly ISqlSugarClient _DB;

        public UsingWireropeMapping(ICurrentUser currentUser, ISqlSugarClient dB)
            : base(currentUser)
        {
            _DB = dB;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override UsingWireropeModel Entity2Model(UsingWirerope entity)
        {
            if (entity == null)
            {
                return null;
            }

            UsingWireropeModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/

            model.Department_Name = entity.Department_Name;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override UsingWirerope Model2Entity(UsingWireropeModel model)
        {
            UsingWirerope entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<UsingWireropeModel> Entity2Model(IEnumerable<UsingWirerope> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<UsingWirerope> Model2Entity(IEnumerable<UsingWireropeModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<UsingWirerope> FilterModel2Where(UsingWireropeFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                if (_currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item1 == false)
                {
                    where = where.And(x => _currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item2.Contains(x.DepartmentID));
                }
            }
            else if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.监管部门)
            {
                where = where.And(x => _currentUser.CurrentUserInfo.LoginRegulatorDepartmentIDs.Contains(x.DepartmentID));
            }

            //数据状态监测不同灯颜色的搜索条件
            if (filterModel._La.IsNotEmpty() && filterModel._La.Count > 0)
            {
                where = where.And(x => x.UsingWireropeID == SqlFunc.Subqueryable<CheckWireropeStatusMonitor>().Where(y => y.La.HasValue && filterModel._La.Contains(y.La.Value)).GroupBy(y => y.UsingWireropeID).Select(y => y.UsingWireropeID));
            }

            if (filterModel._Dia.IsNotEmpty() && filterModel._Dia.Count > 0)
            {
                where = where.And(x => x.UsingWireropeID == SqlFunc.Subqueryable<CheckWireropeStatusMonitor>().Where(y => y.Dia.HasValue && filterModel._Dia.Contains(y.Dia.Value)).GroupBy(y => y.UsingWireropeID).Select(y => y.UsingWireropeID));
            }

            if (filterModel._Lma.IsNotEmpty() && filterModel._Lma.Count > 0)
            {
                where = where.And(x => x.UsingWireropeID == SqlFunc.Subqueryable<CheckWireropeStatusMonitor>().Where(y => y.Lma.HasValue && filterModel._Lma.Contains(y.Lma.Value)).GroupBy(y => y.UsingWireropeID).Select(y => y.UsingWireropeID));
            }

            if (filterModel._ZongHe.IsNotEmpty() && filterModel._ZongHe.Count > 0)
            {
                where = where.And(x => x.UsingWireropeID == SqlFunc.Subqueryable<CheckWireropeStatusMonitor>().Where(y => y.Summary.HasValue && filterModel._ZongHe.Contains(y.Summary.Value)).GroupBy(y => y.UsingWireropeID).Select(y => y.UsingWireropeID));
            }

            if (filterModel._NianJu.IsNotEmpty() && filterModel._NianJu.Count > 0)
            {
                where = where.And(x => x.UsingWireropeID == SqlFunc.Subqueryable<CheckWireropeStatusMonitor>().Where(y => y.NianJu.HasValue && filterModel._NianJu.Contains(y.NianJu.Value)).GroupBy(y => y.UsingWireropeID).Select(y => y.UsingWireropeID));
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            //规格型号
            if (!string.IsNullOrEmpty(filterModel._GuiGe))
            {
                where = where.And(x => x.GuiGe.Contains(filterModel._GuiGe));
                //where = where.And(x => x.GuiGe == filterModel._GuiGe);
            }

            ////直径
            //if (filterModel._Diameter.HasValue)
            //{
            //    where = where.And(x => x.Diameter == filterModel._Diameter.Value);
            //}

            ////绳子条数
            //if (filterModel._Cnt.HasValue)
            //{
            //    where = where.And(x => x.Cnt == filterModel._Cnt.Value);
            //}

            ////公称捻距
            //if (filterModel._NianJu.HasValue)
            //{
            //    where = where.And(x => x.NianJu == filterModel._NianJu.Value);
            //}

            ////长度
            //if (filterModel._LengthNum.HasValue)
            //{
            //    where = where.And(x => x.LengthNum == filterModel._LengthNum.Value);
            //}

            ////安装长度
            //if (filterModel._InstallLengthNum.HasValue)
            //{
            //    where = where.And(x => x.InstallLengthNum == filterModel._InstallLengthNum.Value);
            //}

            ////安装地点
            //if (filterModel._InstallAddressID.HasValue)
            //{
            //    where = where.And(x => x.InstallAddressID == filterModel._InstallAddressID.Value);
            //}

            ////主要负责人
            //if (!string.IsNullOrEmpty(filterModel._LinkUser))
            //{
            //    where = where.And(x => x.LinkUser.Contains(filterModel._LinkUser));
            //    //where = where.And(x => x.LinkUser == filterModel._LinkUser);
            //}

            ////负责人电话
            //if (!string.IsNullOrEmpty(filterModel._LinkPhone))
            //{
            //    where = where.And(x => x.LinkPhone.Contains(filterModel._LinkPhone));
            //    //where = where.And(x => x.LinkPhone == filterModel._LinkPhone);
            //}

            ////换绳原因
            //if (!string.IsNullOrEmpty(filterModel._ChangeReason))
            //{
            //    where = where.And(x => x.ChangeReason.Contains(filterModel._ChangeReason));
            //    //where = where.And(x => x.ChangeReason == filterModel._ChangeReason);
            //}

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}

            ////修改人
            //if (filterModel._UpdateUserID.HasValue)
            //{
            //    where = where.And(x => x.UpdateUserID == filterModel._UpdateUserID.Value);
            //}

            if (filterModel._HoisterNames != null && filterModel._HoisterNames.Count > 0)
            {
                where = where.And(x => filterModel._HoisterNames.Contains(x.HoisterName));
            }

            return where;
        }
        #endregion
    }
}
